<?php

namespace app\index\Controller;
use think\Controller;
use think\Db;//导入数据库类 (DB类)
use think\Session;
class Index  extends Controller
{
    public function index()
    {

    	$data=db('session')->field(['name','id'])->select();
    	// dump($data);
    	$this->assign('data',$data);
        

        // 按热度排序论坛名词
        $ft_topic=db('session')->field(['name','id','ft_topic'])->order('ft_topic desc')->select();
        // dump( $ft_topic);
        $this->assign('ft_topic', $ft_topic);


        //按活跃度论坛排名
         $ft_count=db('user')->field(['nickname','id','ft_count'])->order('ft_count desc')->select();
        // dump( $ft_count);
        $this->assign('ft_count', $ft_count);
   


        //最新帖子排行
          // $newart=db('article')->field(['nickname','id','ft_count'])->order('ft_count desc')->select();

          	$newart=Db::field('article.id aid,article.title,session.name,session.id sid')
			->table('bbs_article article,bbs_session session')
			->where('session.id=article.ses_id')
			->order('article.id desc')
			// ->join('think_card c','a.card_id = c.id')
            ->limit(6)
			->select();
            $this->assign('newart', $newart);


        
           
			
            // 前台遍历友情链接
            $link = db('link')->where('status',1)->select();
            $this->assign('link',$link);
            //前台遍历轮播图
            $loop = db('loop')->select();
            $this->assign('loop',$loop);



            //论坛精华推荐
            $tj=db('article')->where('tj_status',1)->order('id desc')->limit(4)->select();
            $this->assign('tj',$tj);
            // dump($tj);exit;
           
            // 精彩图文
            $tw=db('article')->where('jj_status',1)->limit(1)->find();
            $image=db('article_img')->where('art_id',$tw['id'])->select();
            
            $this->assign('tw',$tw);
            $this->assign('image',$image);
            // dump($tw);
            //社会新闻接口
            $curl = curl_init();
            // var_dump($curl);

            //按关键字查询
            $word = urlencode("宠物");

            //设置APIKEY
            $apikey = "f68dc5bb862b10b3eda925634c5f8cfc";
            //设置URL
            curl_setopt($curl, CURLOPT_URL, "http://api.tianapi.com/social/?key=".$apikey."&num=7&word=".$word);
            //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。 
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

            //curl执行
            $data = curl_exec($curl);

            //关闭curl
            curl_close($curl);

            // 处理JSON数据
            // var_dump($data);exit;//JSON字串
            $jsonObj = json_decode($data);
            // var_dump($jsonObj);
            $newslist = $jsonObj->newslist;
          
            // var_dump( $newslist);
            $this->assign('newslist',$newslist);




            //狗狗论坛
            $dog=db('article')->order('id desc')->limit(5)->select();
            $this->assign('dog',$dog);
            // dump($dog);
            return view('index/index');

    }


      public function search(){
        $data=db('article')
        ->where('title|author','like','%'.input('select').'%')
        ->paginate(10);
        $page = $data->render();
      
        // 模板变量赋值
        // dump($data);exit;

        $this->assign('data', $data);
        $this->assign('page', $page);
      

        return view('index/search');
      }

}
